package cn.com.shadowless.xinning.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.adapter.ContractReviewAdapter;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.ContractReviewInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;
import cn.com.shadowless.xinning.view.activity.OfficeActivity;

/**
 * The type Contract review fragment.
 *
 * @author sHadowLess
 */
public class ContractReviewFragment extends BaseFragment {

    /**
     * The Contract review check id.
     */
    @BindView(R.id.contract_review_checkId)
    TextView contractReviewCheckId;
    /**
     * The Contract review create time.
     */
    @BindView(R.id.contract_review_createTime)
    TextView contractReviewCreateTime;
    /**
     * The Contract review creater.
     */
    @BindView(R.id.contract_review_creater)
    TextView contractReviewCreater;
    /**
     * The Contract review contract num.
     */
    @BindView(R.id.contract_review_contractNum)
    TextView contractReviewContractNum;
    /**
     * The Contract review departmenter.
     */
    @BindView(R.id.contract_review_departmenter)
    TextView contractReviewDepartmenter;
    /**
     * The Contract review department.
     */
    @BindView(R.id.contract_review_department)
    TextView contractReviewDepartment;
    /**
     * The Contract review responser.
     */
    @BindView(R.id.contract_review_responser)
    TextView contractReviewResponser;
    /**
     * The Contract review is renew.
     */
    @BindView(R.id.contract_review_isRenew)
    TextView contractReviewIsRenew;
    /**
     * The Contract review origin contract num.
     */
    @BindView(R.id.contract_review_originContractNum)
    TextView contractReviewOriginContractNum;
    /**
     * The Contract review guy.
     */
    @BindView(R.id.contract_review_Guy)
    TextView contractReviewGuy;
    /**
     * The Contract review main content.
     */
    @BindView(R.id.contract_review_mainContent)
    TextView contractReviewMainContent;
    /**
     * The Contract review end time.
     */
    @BindView(R.id.contract_review_endTime)
    TextView contractReviewEndTime;
    /**
     * The Contract review end tip.
     */
    @BindView(R.id.contract_review_endTip)
    TextView contractReviewEndTip;
    /**
     * The Contract review remark.
     */
    @BindView(R.id.contract_review_remark)
    TextView contractReviewRemark;
    /**
     * The Contract review attach info.
     */
    @BindView(R.id.contract_review_attachInfo)
    RecyclerView contractReviewAttachInfo;
    /**
     * The Contract review check process.
     */
    @BindView(R.id.contract_review_check_process)
    LinearLayout contractReviewCheckProcess;

    private final String url = "api/Contract/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_contract_review_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initContractReviewInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, contractReviewCheckProcess);
        }
    }

    @Override
    protected void initView() {

    }

    private void initContractReviewInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getContractReviewInfo(orderId, userName, new RequestCallBack<ContractReviewInfo>() {
                    @Override
                    public void success(RequestResult<ContractReviewInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.getResult().toString());
                                ContractReviewInfo.DataDTO dataBean = result.getResult().getData();
                                List<ContractReviewInfo.DataDTO.FileListDTO> fileList = dataBean.getFileList();
                                String id = dataBean.getId();
                                String createTime = dataBean.getCreateTimeStr();
                                String createName = dataBean.getCreateUserName();
                                String conCode = dataBean.getConCode();
                                String chargeManStr = dataBean.getChargeManStr();
                                String departmentName = dataBean.getDepartmentName();
                                String responsibleStr = dataBean.getResponsibleStr();
                                String isRenew = dataBean.getIsRenewedStr();
                                String oldCode = dataBean.getOldCode();
                                String partnerName = dataBean.getPartnerName();
                                String conContent = dataBean.getConContent();
                                String expirationDateStr = dataBean.getExpirationDateStr();
                                int remindDay = dataBean.getRemindDay();
                                String remark = dataBean.getRemark();

                                contractReviewCheckId.setText(id);
                                contractReviewCreateTime.setText(createTime);
                                contractReviewCreater.setText(createName);
                                contractReviewContractNum.setText(conCode);
                                contractReviewDepartmenter.setText(chargeManStr);
                                contractReviewDepartment.setText(departmentName);
                                contractReviewResponser.setText(responsibleStr);
                                contractReviewIsRenew.setText(isRenew);
                                contractReviewOriginContractNum.setText(oldCode);
                                contractReviewGuy.setText(partnerName);
                                contractReviewMainContent.setText(conContent);
                                contractReviewEndTime.setText(expirationDateStr);
                                contractReviewEndTip.setText(remindDay + "天");
                                contractReviewRemark.setText(remark);

                                contractReviewAttachInfo.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
                                ContractReviewAdapter contractReviewAdapter = new ContractReviewAdapter(R.layout.fragment_contract_review_item, fileList);
                                contractReviewAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                                    String fileName = fileList.get(position).getFileName();
                                    String fileUrl = fileList.get(position).getFileUrl();
                                    Intent intent = new Intent(mActivity, OfficeActivity.class);
                                    intent.putExtra("fileName", fileName);
                                    intent.putExtra("fileUrl", fileUrl);
                                    mActivity.startActivity(intent);
                                });
                                contractReviewAttachInfo.setAdapter(contractReviewAdapter);
                                contractReviewAdapter.notifyDataSetChanged();
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("暂无订单列表");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
